package nl.sodeso.cubicex.command.assertion;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import nl.sodeso.cubicex.CubicExBaseTestCase;

import org.cubictest.selenium.custom.IElementContext;

import com.thoughtworks.selenium.Selenium;

/**
 * Check if the target match to the specified date
 * 
 * @author c.beaujoin
 * @since 0.0.4
 */
public class AssertDate extends CubicExBaseTestCase {

	/**
	 * {@inheritDoc}
	 */
	public void executeTest(Map<String, String> arguments, IElementContext context, Selenium selenium) throws Exception {
		// Retrieve the parameters.
		final String _locator = getArgTarget();
		final String _format = getArgFormat();
		final Integer _deviation = getArgDeviation();

		// Process the deviation
		Calendar calendar = Calendar.getInstance();
		calendar.roll(Calendar.DAY_OF_YEAR, _deviation);

		// Create the formatted date.
		final String _formattedDate = new SimpleDateFormat(_format).format(calendar.getTime());

		// Retrieve a text.
		String _textOfField = selenium.getText(_locator);
		
		// If Text empty or null
		if (_textOfField == null || _textOfField.equals("")) {
			// Retrieve a field value.
			String _valueOfField = selenium.getValue(_locator);
			assertEquals(_valueOfField, _formattedDate);
		} else {
			assertEquals(_textOfField.contains(_formattedDate), "true");
		}

	}

}
